<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <script src="layui/layui.js"></script>
</head>
<!-- L2Dwidget.js L2D网页动画人物 -->
<script src="https://eqcn.ajz.miesnfu.com/wp-content/plugins/wp-3d-pony/live2dw/lib/L2Dwidget.min.js"></script>
<script>
    L2Dwidget.init({
        "model": {
            "jsonPath": "https://unpkg.com/live2d-widget-model-koharu@1.0.5/assets/koharu.model.json",
            "scale": 1,
            "hHeadPos": 0.5,
            "vHeadPos": 0.618
        },
        "display": {"position": "right", "width": 250, "height": 300, "hOffset": 0, "vOffset": 0},
        "mobile": {"show": true, "scale": 0.5},
        "react": {"opacityDefault": 0.7, "opacityOnHover": 0.2}
    });
</script>
<body>
<div style="margin: 0 40% ;color: #4E5465;font-size: 50px ">生成采购单</div>

<%--<div class="demoTable">--%>
    <%--<div class="layui-inline">--%>
        <%--<input class="layui-input" name="bookname" id="demoReload" autocomplete="off" placeholder="请输入书名">--%>
    <%--</div>--%>
    <%--<button class="layui-btn" data-type="reload">搜索</button>--%>
<%--</div>--%>

<div class="demoTable" style="display: inline-block">
    <form id="AAA" style="display: inline-block">
        <%--<div class="layui-inline">--%>
        <%--<input class="layui-input" name="bookid" placeholder="id" id="demoReload" autocomplete="off">--%>
        <%--</div>--%>
        <div class="layui-inline">
            <input class="layui-input" name="bookname" placeholder="书名" id="demoReload1" autocomplete="off">
        </div>
        <div class="layui-inline">
            <input class="layui-input" name="author" id="demoReload2" placeholder="作者" autocomplete="off">
        </div>
        <div class="layui-inline">
            <input  class="layui-input" name="press" id="demoReload3" placeholder="出版社"
                    autocomplete="off">
        </div>
    </form>
    <button class="layui-btn" data-type="reload"><i class="layui-icon layui-icon-search"></i></button>
</div>
<button class="layui-btn" data-type="reload" id="btn1"><i class="layui-icon layui-icon-refresh"></i></button>


<div class="layui-row">
    <div class="layui-col-xs6">
        <div class="grid-demo grid-demo-bg1">

            <table id="test" lay-filter="test"></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">添加采购订单</button>
                </div>
            </script>
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit1">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>
    </div>
<%--<table id="test" lay-filter="test"></table>--%>
<%--&lt;%&ndash;<table id="test1" lay-filter="test2"></table>&ndash;%&gt;--%>
<%--<script type="text/html" id="toolbarDemo">--%>
    <%--<div class="layui-btn-container">--%>
        <%--<button class="layui-btn layui-btn-sm" lay-event="getCheckData">添加采购订单</button>--%>
    <%--</div>--%>
    <div class="layui-col-xs6">
        <div class="grid-demo">

            <table id="test1" lay-filter="test1"></table>
            <script type="text/html" id="toolbarDemo1">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">添加采购订单</button>
                    <button class="layui-btn layui-btn-sm" lay-event="del">清空采购订单</button>
                </div>
            </script>
            <script type="text/html" id="barDemo1">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>
    </div>
</div>

</body>
<script>
    layui.use(['table', 'jquery', 'form', 'layer'], function () {
        var table = layui.table;
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;


        //页面加载时执行下面两个函数
        $(function () {
            showTable1();
            showTable();
        })
        var v = false;
        var arr = [];
        function  showTable1(){
            table.render({
                elem: '#test'
                , url: '${pageContext.request.contextPath}/book/query'
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    , layEvent: 'LAYTABLE_TIPS'
                    , icon: 'layui-icon-tips'
                }]
                , title: '用户数据表'
                , cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'bookid', title: '书ID', fixed: 'left', unresize: true, sort: true}
                    , {field: 'bookname', title: '书名'}
                    , {field: 'author', title: '作者', sort: true}
                    , {field: 'count', title: '库存'}
                    , {field: 'type', title: '类别'}
                    , {field: 'press', title: '出版社', sort: true}
                    , {field: 'inprice', title: '进价', sort: true}
                ]]
                ,id:'testReload'
                , page: true
                ,limit:8
                ,limits:[8,10,15]
            });
        }

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    $.each(data, function (v, value) {
                        var a = "";
                        for (var i = 0; i < arr.length; i++) {
                            if(arr[i].bookid==value.bookid){
                                a="ok";
                            }
                        }
                        if(a=="ok"){
                            layer.msg("已存在该数据",{icon:4})
                        }else {
                            arr.push(value);
                        }

                    });
                    showTable();
                    break;
            };
        });

        function showTable() {
            table.render({
                elem: '#test1'
                , data: arr
                , toolbar: '#toolbarDemo1' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    , layEvent: 'LAYTABLE_TIPS'
                    , icon: 'layui-icon-tips'
                }]
                , title: '用户数据表'
                , cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'bookid', title: '书ID', fixed: 'left', unresize: true, sort: true}
                    , {field: 'bookname', title: '书名'}
                    , {field: 'author', title: '作者', sort: true}
                    , {field: 'count', title: '库存'}
                    , {field: 'plancount', title: '订购数量', edit: 'text'}
                    , {field: 'type', title: '类别'}
                    , {field: 'press', title: '出版社', sort: true}
                    , {field: 'inprice', title: '进价', sort: true}
                    , {fixed: 'right', title: '操作', toolbar: '#barDemo1'}
                ]]
                , page: false
            });
        }

        //头工具栏事件
        table.on('toolbar(test1)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    var s = "";
                    for (var i = 0; i <data.length; i++) {
                        if(data[i].plancount==undefined){
                            s="ok";
                        }
                    }
                    if(s=="ok"){
                        layer.msg("有数据没有被选中",{icon:5})
                    }else if(v) {
                    $.ajax({
                        type: 'post',
                        url: '${pageContext.request.contextPath}/order/generateOrder',
                        data: JSON.stringify(data),
                        contentType: 'application/json;charset=utf-8',
                        success: function (result) {
                            if (result.code == 1) {
                                layer.msg(result.msg, {icon: 1});
                                arr.splice(0);
                                showTable();
                            } else {
                                layer.msg(result.msg, {icon: 5});
                            }
                        }

                    });
                    }else {
                        layer.msg("数据不合法",{icon:5});
                    }

                    break;
                case 'del':
                    arr.splice(0);
                    showTable();
                    break;
            }

        });


        table.on('edit(test1)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
           v=false;
            var data=obj.data;
            var reg = /^[1-9]\d*$/;
            if (!reg.test(obj.value)) {
                layer.msg("数量为正整数!", {icon: 5, anim: 6});
            }else {
                v= true;
            }
        });
        //监听行工具事件
        table.on('tool(test1)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    for (var i = 0; i < arr.length; i++) {
                        if(arr[i].bookid==data.bookid){
                            arr.splice(i,1);
                            showTable();
                        }
                    }
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.prompt({
                    formType: 2
                    , value: data.email
                }, function (value, index) {
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });


        <%--//表单提交--%>
        <%--form.on('submit(submitForm)', function (data) {--%>
            <%--$.ajax({--%>
                <%--type: 'post',--%>
                <%--url: '${pageContext.request.contextPath}/book/update',--%>
                <%--data: data.field,--%>
                <%--dataType: 'json',--%>
                <%--success: function (result) {--%>
                    <%--layer.closeAll();--%>
                    <%--if (result.code == 1) {--%>
                        <%--//layer.msg  提示框--%>
                        <%--showTable();--%>
                        <%--layer.msg(result.msg);--%>
                    <%--} else {--%>
                        <%--layer.msg(result.msg);--%>
                    <%--}--%>

                <%--}--%>
            <%--});--%>
            <%--return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。--%>
        <%--});--%>


        //条件查询的重载
        var $ = layui.$, active = {
            reload: function () {
                var demoReload = $('#demoReload').val();
                var demoReload1 = $('#demoReload1').val();
                var demoReload2 = $('#demoReload2').val();
                var demoReload3 = $('#demoReload3').val();
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        bookid: demoReload,
                        bookname: demoReload1,
                        author: demoReload2,
                        press: demoReload3
                    }
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $(document).on('click', '#btn1', function () {

            showTable1();
            $("#AAA")[0].reset();

        });

        //
        // //搜索时执行表格重载方法
        // var $ = layui.$, active = {
        //     reload: function () {
        //         var demoReload = $('#demoReload').val();
        //         //执行重载
        //         table.reload('testReload', {
        //             page: {
        //                 curr: 1 //重新从第 1 页开始
        //             }
        //             , where: {
        //                 bookname: demoReload
        //             }
        //         }, 'data');
        //     }
        // };
        //
        // $('.demoTable .layui-btn').on('click', function () {
        //     var type = $(this).data('type');
        //     active[type] ? active[type].call(this) : '';
        // });

    });
</script>
</html>
